package com.example.apprendrechinois.cours;

import java.io.File;
import java.io.FileReader;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.app.ListActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.example.apprendrechinois.R;

public class CoursText extends ListActivity{
	
	private ArrayList<String> coursContextCH = new ArrayList<String>();
	private ArrayList<String> coursContextSon = new ArrayList<String>();
	private ArrayList<String> coursContextFR = new ArrayList<String>();

	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState); 
 	
		try{
			 int i=0;
			 boolean ajout=false, ajoutCH=false, ajoutFR=false, ajoutSon=false;

	         XmlPullParser xpp = this.getResources().getXml(R.xml.text);
	 		 Bundle bundle = this.getIntent().getExtras();
			 int number = bundle.getInt("number");
	         int eventType = xpp.getEventType();
	         while (eventType != XmlPullParser.END_DOCUMENT) {
	          if(eventType == XmlPullParser.START_DOCUMENT) {
	          } else if(eventType == XmlPullParser.START_TAG) {
	        	  if(xpp.getName().equals("textchinois") ||xpp.getName().equals("textfrancais")||xpp.getName().equals("pronociation")){
	        		  ajout = true;
	        		  if(xpp.getName().equals("textchinois")){
	        			  ajoutCH = true;
	        		  }else if (xpp.getName().equals("textfrancais")){
	        			  ajoutFR=true;
	        		  }else if (xpp.getName().equals("pronociation")){
	        			  ajoutSon=true;
	        		  }else{
	        			  ajout=false;
	        		  }
	        	  }
	          } else if(eventType == XmlPullParser.TEXT) {
	        	  if(ajout==true){
	        		  if(ajoutCH==true){
		        		  this.coursContextCH.add(xpp.getText());
	        		  }else if (ajoutFR==true){
		        		  this.coursContextFR.add(xpp.getText());
	        		  }else if (ajoutSon==true){
		        		  this.coursContextSon.add(xpp.getText());
	        		  }else{
	        			  
	        		  }
	        	  }
	          }else if(eventType == XmlPullParser.END_TAG) {
	        	  ajout=false;
	        	  ajoutCH=false;
	        	  ajoutFR=false;
	        	  ajoutSon=false;
	          }
	          eventType = xpp.next();
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		System.out.println(this.coursContextCH.size());
		System.out.println(this.coursContextFR.size());
		System.out.println(this.coursContextSon.size());
	    ArrayList<ACoursText> alist = new ArrayList<ACoursText>();
	    for(int i=0; i<this.coursContextCH.size(); i++){
	    	ACoursText a = new ACoursText(this.coursContextCH.get(i), this.coursContextSon.get(i), this.coursContextFR.get(i));
	    	alist.add(a);
	    }
	    System.out.println(alist.size());
		CoursTextAdapter cta = new CoursTextAdapter(this, alist);
		setListAdapter(cta);
		
    }
}
